<template>
  <el-dialog :visible.sync="value" :before-close="() => $emit('input', false)">
    <template slot="title">
      复制权限
    </template>
    <el-form :model="form" ref="refForm" :rules="rules" label-width="100px">
      <div style="margin-bottom: 20px;">
        <el-alert
          title="确定要将所选中岗位权限复制到此岗位吗？"
          type="info"
          :closable="false">
        </el-alert>
      </div>
      <el-form-item label="岗位选择：" prop="department">
        <el-cascader
          :options="options"
          v-model="form.department"
        ></el-cascader>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('input', false)" :loading="loading">取 消</el-button>
      <el-button type="primary" @click="doSave({action: 'saveForm', form: 'refForm'})" :loading="loading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {authDepartmentAllGetApi, authCopyPermissionApi} from '@/services/api/permission';

  import {form} from '@/build';

  export default {

    name: 'copy-auth-dialog',

    props: {
      value: Boolean,
      id: Number
    },

    mixins: [form],

    data() {
      return {
        form: {
          department: undefined
        },
        options: [],
        rules: {
          department: [
            {required: true, message: '请选择你要复制的岗位'}
          ]
        }
      }
    },
    methods: {
    },
    mounted() {
      this.bindGet(authDepartmentAllGetApi, 'options', {
        format: (list) => {
          return _.map(list, data => {
            return {
              value: data.departmentId,
              label: data.name,
              children: _.map(data.jobList, (childrenInfo) => {
                return {
                  value: childrenInfo.jobId,
                  label: childrenInfo.name
                };
              })
            }
          })
        }
      });
      this.bindSave(authCopyPermissionApi, 'saveForm', 'form', {
        beforeSend: (data) => {
          return {...data, fromId: this.id};
        },
        afterSuccess: () => {
          this.$emit('submit');
        }
      });

      this.getAll();
    },
  }
</script>

<style scoped>
</style>
